package chat.user;

import java.io.Serializable;
import java.net.Socket;
import java.util.*;

public class Group implements Serializable {
	
	private String group_name;
	private Vector<User> user_list = new Vector<User>();
	private ArrayList<Socket> sockets = new ArrayList<Socket>();
	private HashMap<String, Socket> user_socket_list = new HashMap<String, Socket>();
	
	
	public boolean equals(Group g){
		
		if (this.group_name.equalsIgnoreCase(g.getName())){
			
			return true;
		}
		
		return false;
	}
	
	public Group(String name){
		
		group_name = name;
		
	}
	
	public String getName(){
		
		return this.group_name;
	}
	
	public void addUser(User user){
		
		this.user_list.add(user);
	}
	
	public Vector<User> getUsers(){
		
		return this.user_list;
	}
	
	public ArrayList<Socket> getSockets(){
		return sockets;
	}
	
	public boolean addSocket(Socket s){
		return sockets.add(s);
	}
	
	public int getSocketCount(){
		return user_socket_list.size();
	}
	
	public Socket getSocket(User u){
		
		Socket socket = null;
		
		for (User user: this.user_list){
			
			if (user.equals(u)) 
			{
				socket = user.getSocket();
			}
			
		}
		
		return socket; 
	}
	
	public boolean addUserSocket(String usernam, Socket s){
		
		/*Iterator <String> iter = this.user_socket_list.keySet().iterator();
		
		boolean flag=true;
		
		while (iter.hasNext()){
			
			String next = iter.next();
			
			if(next.equalsIgnoreCase(usernam)){flag=false;break;}
			
		}
		
		if (flag){this.user_socket_list.put(usernam, s);}
		
		return flag;*/
		
		boolean flag=true;
		
		for (Map.Entry<String, Socket> entry: user_socket_list.entrySet()){
			
			String key = entry.getKey();
			
			if (key.equalsIgnoreCase(usernam)){flag=false;break;}
		}
		
		if (flag){this.user_socket_list.put(usernam, s);}
		
		return flag;
		
	}
	
	public boolean removeUserSocket(String username){
		
		boolean flag=false;
		
		for (Map.Entry<String, Socket> entry: user_socket_list.entrySet()){
			
			String key = entry.getKey();
			
			if (key.equalsIgnoreCase(username)){
				
				this.user_socket_list.remove(username);
				flag=true;
			}
		}
		
		return flag;
	}
	
	public void removeUser(User u){
		
		this.user_list.remove(u);
	}
	
	public HashMap<String, Socket> getUserSocketList(){
		
		return this.user_socket_list;
	}
	
	public Vector<User> getUserList(){
		
		return this.user_list;
	}
	
}
